package com.si.sea_island.controller;

import com.si.sea_island.base.JsonResult;
import com.si.sea_island.base.ResultCode;
import com.si.sea_island.mapper.*;
import com.si.sea_island.pojo.*;
import com.si.sea_island.pojo.sms.SmsDB;
import com.si.sea_island.utils.DateUtils;
import com.si.sea_island.utils.JwtTokenUtil;
import com.si.sea_island.utils.PrivacyEncryptionUtil;
import com.si.sea_island.utils.WPhoneUtils;
import com.si.sea_island.mapper.UserDBMapper;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Transactional//开启事务回滚
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    UserDBMapper userDBMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    SmsDBMapper smsDBMapper;
    @Autowired
    PrivacyEncryptionUtil privacyEncryptionUtil;

    @ApiImplicitParams({
    })
    @ApiOperation(value = "获取版本号", notes = "获取版本号")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/getVersion")
    public JsonResult getVersion(HttpServletRequest request){

        return JsonResult.success("获取版本号成功").data("200");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "nickName",value = "用户昵称",required = false,paramType = "param"),
            @ApiImplicitParam(name = "realGender",value = "性别",required = false,paramType = "param"),
            @ApiImplicitParam(name = "personalSignature",value = "个性签名",required = false,paramType = "param"),
            @ApiImplicitParam(name = "avatar",value = "用户头像",required = false,paramType = "param"),
    })
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/updateUser")
    public JsonResult updateUser(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-用户
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"user".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        String nickName = request.getParameter("nickName");
        String realGender = request.getParameter("realGender");
        String personalSignature = request.getParameter("personalSignature");
        String avatar = request.getParameter("avatar");
        UserDB user = userDBMapper.getUserById(userId);
        if (nickName==null){
            nickName=user.getNickName();
        }else if (nickName==""){
            return JsonResult.error(2000,"昵称不能为空");
        }
        if (realGender==null){
            realGender=user.getRealGender();
        }else if(realGender==""){
            return JsonResult.error(2000,"性别为空");
        }
        if (personalSignature==null){
            personalSignature=user.getPersonalSignature();
        }
        if (avatar==null){
            avatar = user.getAvatar();
        }else if (avatar==""){
            return JsonResult.error(2000,"头像不能为空");
        }
        userDBMapper.updateUser(userId,nickName,realGender,personalSignature,avatar);
        user = userDBMapper.getUserById(userId);
        user.setToken(token);

        //私密信息加密
        user = privacyEncryptionUtil.privacyEncryption(user);

        return JsonResult.success("修改用户信息成功").data(user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "phone",value = "新手机号",required = false,paramType = "param"),
            @ApiImplicitParam(name = "oldCheckV",value = "原手机号短信验证码",required = true,paramType = "param"),
            @ApiImplicitParam(name = "newCheckV",value = "新手机号短信验证码",required = true,paramType = "param"),
    })
    @ApiOperation(value = "修改绑定手机号", notes = "修改绑定手机号")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/updateUserPhone")
    public JsonResult updateUserPhone(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-用户
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"user".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        String phone = request.getParameter("phone");
        String oldCheckV = request.getParameter("oldCheckV");
        String newCheckV = request.getParameter("newCheckV");
        UserDB user = userDBMapper.getUserById(userId);
        if (!WPhoneUtils.isChinaPhone(phone)) {
            return JsonResult.error(ResultCode.phone_format_error,"请填写正确的手机号");
        }
        if (phone==null||"".equals(phone)){
            phone=user.getPhone();
        }else if(!user.getPhone().equals(phone)){
            if (newCheckV==null||"".equals(newCheckV)||oldCheckV==null||"".equals(oldCheckV)){
                return JsonResult.error(2000,"缺少短信验证码");
            }
            //短信校验(旧手机号)
            SmsDB ordSmsDB = smsDBMapper.getSmsByPhone(user.getPhone());
            if (ordSmsDB == null) {
                return JsonResult.error(ResultCode.sms_not_send,"未发送短信到"+phone);
            }
            //短信时间校验
            if (System.currentTimeMillis() - Long.parseLong(ordSmsDB.getTime()) > 900000) {
                return JsonResult.error(ResultCode.sms_time_out,"验证码超时");
            }
            if (!ordSmsDB.getCode().equals(newCheckV)) {
                return JsonResult.error(ResultCode.sms_verify_error,"短信验证码错误");
            }

            //短信校验(新手机号)
            ordSmsDB = smsDBMapper.getSmsByPhone(phone);
            if (ordSmsDB == null) {
                return JsonResult.error(ResultCode.sms_not_send,"未发送短信到"+phone);
            }
            //短信时间校验
            if (System.currentTimeMillis() - Long.parseLong(ordSmsDB.getTime()) > 900000) {
                return JsonResult.error(ResultCode.sms_time_out,"验证码超时");
            }
            if (!ordSmsDB.getCode().equals(newCheckV)) {
                return JsonResult.error(ResultCode.sms_verify_error,"短信验证码错误");
            }

            UserDB userDB = userDBMapper.selectUserByUsername(phone);
            if (!userDB.getUserName().equals(phone)){
                return JsonResult.error(2000,"手机号已被使用");
            }
        }
        userDBMapper.updateUserPhone(userId,phone);
        user = userDBMapper.getUserById(userId);
        user.setToken(token);

        //私密信息加密
        user = privacyEncryptionUtil.privacyEncryption(user);

        return JsonResult.success("修改用户信息成功").data(user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "phone",value = "手机号",required = true,paramType = "param"),
            @ApiImplicitParam(name = "checkV",value = "短信验证码）",required = true,paramType = "param"),
    })
    @ApiOperation(value = "验证手机号", notes = "验证手机号")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/checkPhone")
    public JsonResult checkPhone(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        String phone = request.getParameter("phone");
        String checkV = request.getParameter("checkV");
        if (checkV==null||"".equals(checkV)){
            return JsonResult.error(2000,"请输入短信验证码");
        }
        if (phone==null||"".equals(phone)){
            return JsonResult.error(2000,"请输入手机号");
        }
        //短信校验
        SmsDB ordSmsDB = smsDBMapper.getSmsByPhone(phone);
        if (ordSmsDB == null) {
            return JsonResult.error(ResultCode.sms_not_send,"未发送短信到"+phone);
        }
        //短信时间校验
        if (System.currentTimeMillis() - Long.parseLong(ordSmsDB.getTime()) > 900000) {
            return JsonResult.error(ResultCode.sms_time_out,"验证码超时");
        }
        if (!ordSmsDB.getCode().equals(checkV)) {
            return JsonResult.error(ResultCode.sms_verify_error,"短信验证码错误");
        }
        return JsonResult.success("验证成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
    })
    @ApiOperation(value = "获取用户详情", notes = "获取用户详情")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/getUser")
    public JsonResult getUser(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null||"".equals(token)){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        UserDB user = userDBMapper.getUserById(userId);
        user.setToken(token);

        //私密信息加密
        user = privacyEncryptionUtil.privacyEncryption(user);

        return JsonResult.success("获取用户详情成功").data(user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "checkV",value = "短信验证码",required = true,paramType = "param"),
            @ApiImplicitParam(name = "payPassword",value = "支付密码",required = true,paramType = "param"),
    })
    @ApiOperation(value = "设置/修改支付密码", notes = "设置/修改支付密码")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/setPayPassword")
    public JsonResult setPayPassword(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        UserDB user = userDBMapper.getUserById(userId);

        String phone =user.getPhone();
        String checkV = request.getParameter("checkV");
        String payPassword = request.getParameter("payPassword");
        if (checkV==null||"".equals(checkV)){
            return JsonResult.error(2000,"请输入短信验证码");
        }
        if (payPassword==null||"".equals(payPassword)){
            return JsonResult.error(2000,"请输入支付密码");
        }
        try{
            String[] split = payPassword.split("");
            if (split.length!=6){
                return JsonResult.error(ResultCode.sms_not_send,"请输入六位纯数字支付密码");
            }
            Integer integer = Integer.valueOf(payPassword);
            if (integer<=100000||integer>999999){
                return JsonResult.error(ResultCode.sms_not_send,"请输入六位纯数字支付密码");
            }
        }catch (Exception e){
            return JsonResult.error(ResultCode.sms_not_send,"请输入六位纯数字支付密码");
        }
        //短信校验
        SmsDB ordSmsDB = smsDBMapper.getSmsByPhone(phone);
        if (ordSmsDB == null) {
            return JsonResult.error(ResultCode.sms_not_send,"未发送短信到"+phone);
        }
        //短信时间校验
        if (System.currentTimeMillis() - Long.parseLong(ordSmsDB.getTime()) > 900000) {
            return JsonResult.error(ResultCode.sms_time_out,"验证码超时");
        }
        if (!ordSmsDB.getCode().equals(checkV)) {
            return JsonResult.error(ResultCode.sms_verify_error,"短信验证码错误");
        }

        user.setPayPassword(payPassword);

        userDBMapper.updatePayPassword(user);

        return JsonResult.success("设置支付密码成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "oldPassword",value = "旧密码",required = true,paramType = "param"),
            @ApiImplicitParam(name = "newPassword",value = "新密码",required = true,paramType = "param"),
    })
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/updatePassword")
    public JsonResult updatePassword(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        UserDB user = userDBMapper.getUserById(userId);

        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        if (oldPassword==null || "".equals(oldPassword)){
            return JsonResult.error(2000,"请输入旧密码");
        }
        if (newPassword==null || "".equals(newPassword)){
            return JsonResult.error(2000,"请输入新密码");
        }

        if (!passwordEncoder.matches(oldPassword,user.getPassword())){
            return JsonResult.error(ResultCode.password_error,"旧密码错误");
        }

        userDBMapper.updatePassword(newPassword,userId);

        return JsonResult.success("修改密码成功");
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "phone",value = "手机号",required = true,paramType = "param"),
            @ApiImplicitParam(name = "checkV",value = "短信验证码",required = true,paramType = "param"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "param"),
    })
    @ApiOperation(value = "忘记密码", notes = "忘记密码")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/forgotPassword")
    public JsonResult forgotPassword(HttpServletRequest request){
        //校验参数
        String checkV = request.getParameter("checkV");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        if (checkV==null||"".equals(checkV)){
            return JsonResult.error(2000,"请输入短信验证码");
        }
        if (password==null||"".equals(password)){
            return JsonResult.error(2000,"请输入支付密码");
        }
        if (phone==null||"".equals(phone)){
            return JsonResult.error(2000,"请输入支付密码");
        }
        UserDB user = userDBMapper.getUserByPhone(phone);
        if (user==null){
            return JsonResult.error(2000,"用户不存在，请先注册");
        }
        //短信校验
        SmsDB ordSmsDB = smsDBMapper.getSmsByPhone(phone);
        if (ordSmsDB == null) {
            return JsonResult.error(ResultCode.sms_not_send,"未发送短信到"+user.getPhone());
        }
        //短信时间校验
        if (System.currentTimeMillis() - Long.parseLong(ordSmsDB.getTime()) > 900000) {
            return JsonResult.error(ResultCode.sms_time_out,"验证码超时");
        }
        if (!ordSmsDB.getCode().equals(checkV)) {
            return JsonResult.error(ResultCode.sms_verify_error,"短信验证码错误");
        }

        userDBMapper.updatePassword(passwordEncoder.encode(password),user.getId());

        return JsonResult.success("重置密码成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "realName",value = "真实姓名",required = true,paramType = "param"),
            @ApiImplicitParam(name = "iDCard",value = "身份证",required = true,paramType = "param"),
    })
    @ApiOperation(value = "实名认证", notes = "实名认证")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/realNameAuthentication")
    public JsonResult realNameAuthentication(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        UserDB user = userDBMapper.getUserById(userId);

        String realName = request.getParameter("realName");
        String iDCard = request.getParameter("iDCard");
        if (realName==null||"".equals(realName)){
            return JsonResult.error(2000,"请输入真实姓名");
        }
        if (iDCard==null||"".equals(iDCard)){
            return JsonResult.error(2000,"请输入身份证");
        }

        userDBMapper.realNameAuthentication(realName,iDCard,user.getId());

        return JsonResult.success("实名认证申请已提交,工作人员会在1~2个工作日内处理，请耐心等待");
    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "payPassword",value = "支付密码",required = true,paramType = "param"),
    })
    @ApiOperation(value = "校验支付密码", notes = "校验支付密码")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/inspectPayPassword")
    public synchronized JsonResult inspectPayPassword(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        UserDB user = userDBMapper.getUserById(userId);

        String payPassword = request.getParameter("payPassword");
        if (payPassword==null||"".equals(payPassword)){
            return JsonResult.error(2000,"请输入支付密码");
        }
        if (payPassword.equals(user.getPayPassword())){
            return JsonResult.error(2000,"密码错误");
        }
        return JsonResult.error(2000,"密码正确");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "phone",value = "电话",required = true,paramType = "param"),
    })
    @ApiOperation(value = "根据电话查询用户信息", notes = "根据电话查询用户信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/getUserInformationByPhone")
    public synchronized JsonResult getUserInformationByPhone(HttpServletRequest request){

        String phone = request.getParameter("phone");
        if (phone==null||"".equals(phone)){
            return JsonResult.error(2000,"缺少参数");
        }

        UserDB user = userDBMapper.getUserByPhone(phone);
        if (user==null){
            return JsonResult.error(2000,"用户不存在");
        }

        return JsonResult.success("根据电话查询用户信息成功").data(user);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
    })
    @ApiOperation(value = "每日任务提交", notes = "每日任务提交")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/dailyTasksSubmit")
    public synchronized JsonResult dailyTasksSubmit(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        UserDB user = userDBMapper.getUserById(userId);

        //检查次数
        int dailyTasksNumber = Integer.valueOf(user.getDailyTasksNumber());
        if (dailyTasksNumber<=0){
            return JsonResult.error(ResultCode.param_is_null,"今日次数已耗尽，请明日再来");
        }
        //增加积分
        String integral = new BigDecimal(user.getIntegral()).add(new BigDecimal(0.1)).toString();
        userDBMapper.dailyTasksSubmit(userId,integral,dailyTasksNumber-1);

        return JsonResult.error(2000,"积分领取成功");
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "aliPayAccount",value = "支付宝提现账号",required = true,paramType = "param"),
            @ApiImplicitParam(name = "aliPayRealName",value = "支付宝提现实名信息",required = true,paramType = "param"),
    })
    @ApiOperation(value = "设置支付宝提现账户", notes = "客服发送聊天")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/setAliPayAccount")
    public JsonResult setAliPayAccount(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        String userId = String.valueOf(tokenClaims.get("userId"));

        String aliPayAccount = request.getParameter("aliPayAccount");
        String aliPayRealName = request.getParameter("aliPayRealName");
        if (aliPayAccount==null||"".equals(aliPayAccount)) {
            return JsonResult.error(ResultCode.phone_format_error,"缺少参数");
        }
        if (aliPayRealName==null||"".equals(aliPayRealName)) {
            return JsonResult.error(ResultCode.phone_format_error,"缺少参数");
        }

        userDBMapper.setAliPayAccount(userId,aliPayAccount,aliPayRealName);

        return JsonResult.success("设置成功");
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
    })
    @ApiOperation(value = "获取团队信息", notes = "获取团队信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/getTeamInformation")
    public JsonResult getTeamInformation(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        String userId = String.valueOf(tokenClaims.get("userId"));

        Map<String, Object> map = new HashMap<>();
        //团队人数
        int teamNumber = userDBMapper.getTeadNumber(","+userId+",");
        map.put("teamNumber", teamNumber);
        //直推人数
        int directPushNumber = userDBMapper.getDirectPushNumber(","+userId+",");
        map.put("directPushNumber", directPushNumber);
        //团队有效会员数
        int vipNumber = userDBMapper.getVipNumber(","+userId+",");
        map.put("vipNumber", vipNumber);
        //直推业绩
        String directPushPerformance = userDBMapper.getDirectPushPerformance(userId);
        map.put("directPushPerformance", directPushPerformance);
        //间推业绩
        String interPushPerformance = userDBMapper.getInterPushPerformance(userId);
        map.put("interPushPerformance", interPushPerformance);
        //团队业绩
        try{
            map.put("teamPerformance", new BigDecimal(directPushPerformance).add(new BigDecimal(interPushPerformance)).toString());
        }catch (Exception e){
            map.put("teamPerformance",0);
        }
        //直推列表
        List<UserDB> directPushList = userDBMapper.getDirectPushList(userId);
        map.put("directPushList", directPushList);
        //间推列表
        List<UserDB> interPushList = userDBMapper.getInterPushList(userId,","+userId+",");
        map.put("interPushList", interPushList);

        return JsonResult.success("获取团队信息").data(map);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "phone",value = "支付宝提现账号",required = true,paramType = "param"),
    })
    @ApiOperation(value = "根据电话查询用户头像和昵称", notes = "根据电话查询用户头像和昵称")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/api/getUserByPhone")
    public JsonResult getUserByPhone(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        String userId = String.valueOf(tokenClaims.get("userId"));

        String phone = request.getParameter("phone");
        if (phone==null||"".equals(phone)) {
            return JsonResult.error(ResultCode.phone_format_error,"缺少参数");
        }

        UserDB user = userDBMapper.getUserByPhone(phone);
        if (user==null) {
            return JsonResult.error(ResultCode.phone_format_error,"用户不存在");
        }

        UserDB userDB = new UserDB();
        userDB.setAvatar(user.getAvatar());
        userDB.setNickName(user.getNickName());

        return JsonResult.success("查询用户头像和昵称成功").data(userDB);
    }

}
